package com.example.demo.controller;

import com.example.demo.pojo.dbItems;
import com.example.demo.pojo.dbRole;
import com.example.demo.service.ItemsService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    ItemsService itemsService;

    @GetMapping("/show")
    public Result SelectAll(HttpServletRequest request){
        //判断是否有权限进入管理系统
        HttpSession session=request.getSession();
        Object admin = session.getAttribute("user_name");
        if (admin!=null)
            return Result.success("登陆成功",admin);
        return Result.fail("请登录");
    }

    @GetMapping("selectAll")
    public Result selectAll(){
        Result result=itemsService.selectAll();
        return result;
    }

    @GetMapping("selectByRole")
    public Result selectByRole(dbRole dbRole){
        Result result=itemsService.selectByRole(dbRole);
        return result;
    }

    @GetMapping("selectItemById")
    public Result selectItemById(Integer id){
        Result result=itemsService.selectItemById(id);
        return result;
    }

    @GetMapping("/add")
    public Result add(dbItems item){
        System.out.println(item);
        Result result=itemsService.add(item);
        return result;
    }

    @GetMapping("/update")
    public Result update(dbItems item){
        Result result=itemsService.update(item);
        return result;
    }

    @GetMapping("/delete")
    public Result delete(Integer ID){
        Result result=itemsService.delete(ID);
        return result;
    }
}
